//
//  AppDelegate.m
//  ZLShang
//
//  Created by wangguangming on 15/7/23.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

#import <PgySDK/PgyManager.h>

#import "Reachability.h"

@interface AppDelegate ()
{
    NSTimer *_timer;
     
}
@property(nonatomic,strong)AVAudioPlayer *audioplayer;

@property (nonatomic,retain)Reachability *res;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
        // NSLog(@"frame=%@",NSStringFromCGRect([UIScreen mainScreen].bounds));
        // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    ViewController *viewController=[[ViewController alloc] init];
    UINavigationController *VC=[[UINavigationController alloc] initWithRootViewController:viewController];
   VC.title=@"首页";
    
    
    
    self.window.rootViewController=VC;
    
    
        //添加网络变化的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:kReachabilityChangedNotification object:nil];
    self.res=[Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    [self.res startNotifier];//开始监听网络请求的变化
    
        //注册推送   

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
       
    [[PgyManager sharedPgyManager] checkUpdate];
    
        // 设置用户反馈界面激活方式为摇一摇
    [[PgyManager sharedPgyManager] setFeedbackActiveType:kPGYFeedbackActiveTypeShake];
    [[PgyManager sharedPgyManager] setEnableFeedback:YES];
    [[PgyManager sharedPgyManager] setShakingThreshold:8.0];
        //蒲公英
     [[PgyManager sharedPgyManager] startManagerWithAppId:@" e5f283f4b9eeb04445b8cbb29e4531d7"];
    
    [self.window makeKeyAndVisible];
    return YES;

    
}
- (void)reportException
{
    @try {
        NSArray *array = [NSArray arrayWithObjects:@"", nil];
        NSString *value = [array objectAtIndex:10];
    }
    @catch (NSException *exception) {
        [[PgyManager sharedPgyManager] reportException:exception];
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    
    
          // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
   
        //将要进入后台
                 
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
        //NSLog(@"deviceToken:== %@", deviceToken); 
     
   
        //1.写入路径
    NSString *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
   
        //2.拼接文件路径
    NSString *file=[documents stringByAppendingString:@"/devetoken.plist"];
    NSMutableArray *array=[NSMutableArray arrayWithObjects:deviceToken, nil];
   
    [array writeToFile:file atomically:YES ];
    
    NSMutableArray *a=[NSMutableArray arrayWithContentsOfFile:file];
    
        // NSLog(@"%@",a[0]);
    
} 



- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{  
    
        //NSLog(@"userInfo == %@",userInfo);  
    NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];  
    
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    AudioServicesPlaySystemSound(1007);
    
   [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"]objectForKey:@"badge"] integerValue]];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",  nil];  
    
    [alert show]; 
}  

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{  
        // NSLog(@"Regist fail%@",error);  
} 

-(void)change:(NSNotificationCenter *)notify
{//当网络发生变化的时候，都会触发这个事件
 // NSLog(@"%@",notify);
    
    if([Reachability reachabilityForLocalWiFi].currentReachabilityStatus==NotReachable&&[[Reachability reachabilityForInternetConnection] currentReachabilityStatus]==NotReachable){
       UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"没有网络" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil]
        ;
        [a show];
    }
}
@end
